import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisDemoString {
    public static void main(String[] args) {
        // 1. 连接到 Redis 服务器上.
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        // 2. 从 Redis 连接池中取一个连接出来，并及时释放
        try(Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);

        }
    }

    private static void test4(Jedis jedis) {
        System.out.println("incr n decr");
        jedis.flushAll();

        jedis.set("key","100");

        System.out.println(jedis.incr("key"));

        System.out.println(jedis.decr("key"));

        System.out.println(jedis.incrBy("key", 10));

        System.out.println(jedis.decrBy("key", 10));
    }

    private static void test3(Jedis jedis) {
        System.out.println("append");
        jedis.flushAll();

        jedis.set("key","hello world");
        jedis.append("key"," of popcorn");
        System.out.println("value: "+jedis.get("key"));
    }

    private static void test2(Jedis jedis) {
        System.out.println("setrange n getrange");
        jedis.flushAll();

        jedis.set("key","hello world");
        String range = jedis.getrange("key",0,5);
        System.out.println(range);

        jedis.setrange("key",1,"ooo");
        System.out.println(jedis.get("key"));

    }

    private static void test1(Jedis jedis) {
        System.out.println("mget n mset");
        jedis.flushAll();

        jedis.mset("key1","111","key2","222","key3","333");
        List<String> values = jedis.mget("key1", "key2", "key3");
        System.out.println(values);


    }
}
